package com.demo2do.fms.admin.entity.report.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class LoadPlaceReportSearch implements QueryBuilder {
    
    /** 装卸地名称 */
    private String facilityName;
    
    /** 装卸地所在城市 */
    private String cityName;
    
    /** 导出条数上限 */
    private Integer limit;
    
    private QueryOrder order;
        
    private QueryFilter filter;

    /**
     * The default constructor
     */
    public LoadPlaceReportSearch(){
        order = new QueryOrder();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {

            filter = new QueryFilter().addLike("lp", "facility_name", facilityName)
                                      .addLike("c", "city_name", cityName);
            
        }
        
        return filter;
         
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        String sentence = "SELECT lp.facility_code AS facilityCode, lp.facility_name AS facilityName, c.city_name AS cityName "
                        + "FROM vw_load_place lp, vw_city c "
                        + "WHERE lp.geo_id = c.id AND lp.is_disabled = 0"
                        + getPreparedFilter(false).clauseSentence() + order.sentence();
        
        sentence = "SELECT * FROM (" + sentence + ") WHERE ROWNUM <=" + limit;
        
        return sentence;
        
    }

    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
            
            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("facilityCode", rs.getObject("facilityCode"));
                result.put("facilityName", rs.getObject("facilityName"));
                result.put("cityName", rs.getObject("cityName"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @return
     */
    public QueryFilter getFilter() {
        return getPreparedFilter(false);
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }

    /**
     * @return the facilityName
     */
    public String getFacilityName() {
        return facilityName;
    }

    /**
     * @return the cityName
     */
    public String getCityName() {
        return cityName;
    }

    /**
     * @return the limit
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * @param facilityName the facilityName to set
     */
    public void setFacilityName(String facilityName) {
        this.facilityName = facilityName;
    }

    /**
     * @param cityName the cityName to set
     */
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

}
